package com.trtan.hr.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.trtan.hr.pojo.*;
import com.trtan.hr.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.logging.Logger;

/**
 * @author: trtan
 * @create: 2021-03-07 15:54
 * @description: 菜单管理后端接口
 **/
@RestController
@RequestMapping("/system/menu")
public class MenuController {
    private final Logger logger = Logger.getLogger("MenuController");
    @Autowired
    MenuService menuService;
    /**
     * @author trtan
     * @date 2021/4/7 16:10
     * description: 查询 
     * @param jsonParams
     * @return com.trtan.hr.pojo.RespBean
     */
    @RequestMapping("/getAllMenus")
    public RespBean getAllMenus(@RequestBody JSONObject jsonParams) {
        logger.info(jsonParams.toString());
        String menuName = jsonParams.getString("menuName");
        Integer currentPage = jsonParams.getInteger("currentPage");
        Integer pageSize = jsonParams.getInteger("pageSize");
        RespBean respBean;
        try {
            RespPageEntity allMenus = menuService.getAllMenus(currentPage, pageSize, menuName);
            respBean = RespBean.ok("", allMenus);
        } catch (Exception e) {
            respBean = RespBean.error("服务错误，请联系管理员！");
        }
        return respBean;
    }
    
    @RequestMapping("/deleteMenu")
    public RespBean deleteMenu(@RequestBody JSONObject jsonParams) {
        Integer menu_id = Integer.valueOf(jsonParams.getString("menu_id"));
        RespBean respBean = null;
        try {
            menuService.deleteMenu(menu_id);
            respBean = RespBean.ok("删除成功！");
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/addMenu")
    public RespBean addMenu(@RequestBody JSONObject jsonParams) {
        Menu menu = JSON.toJavaObject(jsonParams, Menu.class);
        RespBean respBean = null;
        try {
            menu.setUrl(menu.getPath() + "/**");
            menuService.addMenu(menu);
            respBean = RespBean.ok("添加成功！", menu.getId());
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/modifyMenu")
    public RespBean modifyMenu(@RequestBody JSONObject jsonParams) {
        Menu menu = JSON.toJavaObject(jsonParams, Menu.class);
        RespBean respBean = null;
        try {
            menu.setUrl(menu.getPath() + "/**");
            menuService.modifyMenu(menu);
            respBean = RespBean.ok("修改成功！", menu.getId());
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/getMenuTree")
    public RespBean getMenuTree(@RequestBody JSONObject jsonParams) {
        Integer role_id = jsonParams.getInteger("role_id");
        RespBean respBean = null;
        try {
            Map<String, Object> result = menuService.getMenuTree(role_id);
            respBean = RespBean.ok("", result);
        } catch (Exception e) {
            respBean = RespBean.error("获取菜单树失败，请联系管理员！");
        }
        return respBean;

    }
}
